package com.example.weather;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.boyuan.huijiaoyu.R;
import com.magicsoft.app.helper.SharePreferenceHelper;
import com.magicsoft.app.wcf.HomeService;
import com.magicsoft.app.wcf.listener.GetOneRecordListener;
import com.magicsoft.constant.Constant;

public class ChangeCityWeatherActivity extends Activity implements OnClickListener {

	private EditText mEdi_search;
	private TextView mTv_change_city_title;
	private GridView mHotCityGridView;
	private List<Hotcity> HotCityList = new ArrayList<Hotcity>();
	private List<Allcity>  AllcityList = new ArrayList<Allcity>();
	private List<Allcity> list_search = new ArrayList<Allcity>();
	
	private MyHotCityAdapter myHotCityAdapter;
	private ListView mListView_search;
	private CitySearchAdapter mySearchAdapter;
	private TextView tv_Noresult;
	private RelativeLayout mRela_hotcity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_city_weather);
		HotCityList = SharePreferenceHelper.getHotCityList(ChangeCityWeatherActivity.this);
		if(HotCityList.size()==0){
			getHotList();
		}
		AllcityList = SharePreferenceHelper.getAllCityList(ChangeCityWeatherActivity.this);
		if(AllcityList.size()==0){
			getAllList();
		}
		
		postDelayed();

	}

	

	private void postDelayed() {
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				initUI();
			}
		}, 1000);
	}
	
	private void getAllList() {
		HomeService homeService = new HomeService(
				ChangeCityWeatherActivity.this);
		homeService.getAllCitylist(new GetOneRecordListener<List<Allcity>>() {
			
			@Override
			public void onFinish(List<Allcity> data) {
				AllcityList.clear();
				if (data != null) {
					AllcityList.addAll(data);
					Log.e("ChangeCityWeatherActivity", "getAllList AllcityList="+AllcityList.size());
					SharePreferenceHelper.saveAllcityList(ChangeCityWeatherActivity.this, AllcityList);
				}
			}
			
			@Override
			public void onFailed(String Message) {
				
			}
		});
	}
	
	private void getHotList() {
		HomeService homeService = new HomeService(
				ChangeCityWeatherActivity.this);
		homeService.getHotcitylist(new GetOneRecordListener<List<Hotcity>>() {

			@Override
			public void onFinish(List<Hotcity> data) {
				HotCityList.clear();
				if (data != null) {
					HotCityList.addAll(data);
					SharePreferenceHelper.saveHotcityList(ChangeCityWeatherActivity.this, HotCityList);
				}
			}

			@Override
			public void onFailed(String Message) {

			}
		});
	}

	private void initUI() {
		mEdi_search = (EditText) findViewById(R.id.edi_search);
		mTv_change_city_title = (TextView) findViewById(R.id.tv_change_city_title);
		mHotCityGridView = (GridView) findViewById(R.id.gridView1);
		mListView_search = (ListView) findViewById(R.id.list_search);
		//城市列表
		myHotCityAdapter = new MyHotCityAdapter();
		mHotCityGridView.setAdapter(myHotCityAdapter);
		//搜索列表
		mySearchAdapter = new CitySearchAdapter();
		mListView_search.setAdapter(mySearchAdapter);
		
		mRela_hotcity = (RelativeLayout) findViewById(R.id.rela_hotcity);
		
		tv_Noresult = (TextView) findViewById(R.id.Tv_Noresult);
		
		findViewById(R.id.changecity_back).setOnClickListener(this);
		String currentCity = SharePreferenceHelper.getCurrentCity(ChangeCityWeatherActivity.this);
		mTv_change_city_title.setText("当前城市——"+currentCity);
		
		mHotCityGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				Hotcity hotcity = HotCityList.get(position);
				String cityname = hotcity.getName();
				Log.e("ChangeCityWeatherActivity", "cityname ="+cityname);
				SharePreferenceHelper.setCurrentCity(cityname, ChangeCityWeatherActivity.this);
				Intent data = new Intent();
				data.putExtra(Constant.ChangeCityName, cityname);
				setResult(Constant.JumpChangeCity_Code, data );
				finish();
			}
		});
		
		mListView_search.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				Allcity allcity = list_search.get(position);
				String cityname = allcity.getName();
				Log.e("ChangeCityWeatherActivity", "cityname ="+cityname);
				SharePreferenceHelper.setCurrentCity(cityname, ChangeCityWeatherActivity.this);
				Intent data = new Intent();
				data.putExtra(Constant.ChangeCityName, cityname);
				setResult(Constant.JumpChangeCity_Code, data );
				finish();
			}
		});
		
		mEdi_search.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
				list_search.clear();
				for (int i = 0; i < AllcityList.size(); i++) {
					if (AllcityList.get(i).getPinyin().contains(s)
							|| AllcityList.get(i).getName()
									.contains(s.toString())) {
						list_search.add(AllcityList.get(i));
					}
				}
				if (list_search.size() == 0) {
					mListView_search.setVisibility(View.GONE);
					mHotCityGridView.setVisibility(View.VISIBLE);
					tv_Noresult.setVisibility(View.VISIBLE);
					mHotCityGridView.setVisibility(View.GONE);
					mRela_hotcity.setVisibility(View.GONE);
				} else {
					mListView_search.setVisibility(View.VISIBLE);
					mySearchAdapter.notifyDataSetChanged();
					mHotCityGridView.setVisibility(View.GONE);
					tv_Noresult.setVisibility(View.GONE);
					mRela_hotcity.setVisibility(View.GONE);
				}
				if ("".equals(s.toString())) {
					mListView_search.setVisibility(View.GONE);
					mHotCityGridView.setVisibility(View.VISIBLE);
					mRela_hotcity.setVisibility(View.VISIBLE);
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {
				
			}
		});
		
	}

	class MyHotCityAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return HotCityList.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View controview, ViewGroup arg2) {
			View view = null;
			ViewHolder viewHolder = null;
			 Hotcity hotcity = HotCityList.get(position);
			 
			if (controview == null) {
				viewHolder = new ViewHolder();
				view = getLayoutInflater().inflate(
						R.layout.hotcity_listliem, null);
				viewHolder.Tv_cityname = (TextView) view.findViewById(R.id.Tv_cityname);
				view.setTag(viewHolder);
			} else {
				view = controview;
				viewHolder = (ViewHolder) view.getTag();
			}
			viewHolder.Tv_cityname.setText(hotcity.getName()+"市");
			return view;
		}

	}
	
	class ViewHolder {
		TextView Tv_cityname;
	}
	
	class CitySearchAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list_search.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View controview, ViewGroup arg2) {
			View view = null;
			ViewHolder viewHolder = null;
			 Allcity allcity = list_search.get(position);
			 
			if (controview == null) {
				viewHolder = new ViewHolder();
				view = getLayoutInflater().inflate(
						R.layout.city_search_list_item, null);
				viewHolder.Tv_cityname = (TextView) view.findViewById(R.id.Tv_city_search_name);
				view.setTag(viewHolder);
			} else {
				view = controview;
				viewHolder = (ViewHolder) view.getTag();
			}
			viewHolder.Tv_cityname.setText(allcity.getName()+"市");
			return view;
		}
		
	}
	
	
	
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.changecity_back:
			finish();
			break;

		default:
			break;
		}
	}
}
